﻿using Common.BaseLibrary.Utils;
using Solution.WebApp.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Solution.WebApp.Commons
{
    public class WeatherManager
    {
        /// <summary>
        /// 线程锁
        /// </summary>
        private static object lockObj = new object();
        /// <summary>
        /// 天气工厂
        /// </summary>
        private static WeatherManager instance;

        public static WeatherManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockObj)
                    {
                        if (instance == null)
                        {
                            instance = new WeatherManager();
                        }
                    }
                }
                return instance;
            }
        }

        public WeatherJSON GetWeather(string cityCode)
        {
            string weatherJson = HttpHelper.GetHttpGetResult("http://www.weather.com.cn/data/cityinfo/" + cityCode + ".html");
            WeatherJSON weather = SerializeMemoryHelper.DeserializeFromJson<WeatherJSON>(weatherJson);

            if (weather.weatherinfo.weather.Contains("晴"))
            {
                weather.weatherIcon = "wi-day-sunny";
                weather.weatherImg = @"~/Assets/img/flat-sun.jpg";
            }
            else if (weather.weatherinfo.weather.Contains("雨"))
            {
                weather.weatherIcon = "wi-day-sunny";
                weather.weatherImg = @"~/Assets/img/flat-sun.jpg";
            }
            else if (weather.weatherinfo.weather.Contains("霾"))
            {
                weather.weatherIcon = "wi-day-sunny";
                weather.weatherImg = @"~/Assets/img/flat-sun.jpg";
            }
            return weather;
        }

    }
}